import 'dart:convert';
import 'dart:io';

import 'package:ad_common/third_ad.dart';
import 'package:ad_common/third_ad_instance.dart';
import 'package:ad_plugin/ad_container.dart';
import 'package:ad_plugin/ad_manager.dart';
import 'package:flutter/foundation.dart';

import 'ad_error.dart';
import 'ad_listener.dart';
import 'ad_source.dart';

/// FileName reward_video_ad
/// @Author zhuqingfang
/// @Date 2022/9/27 10:03 上午
/// @Description reward video ad
class RewardVideoAd extends Ad {
  final AdLoadCallback<RewardVideoAd> adLoadCallback;
  RewardVideoContentCallback<RewardVideoAd>? rewardVideoContentCallback;
  //保存从sdk返回的额外信息，比如广告价值信息
  Map<String,Object>? loadExtraInfoMap;
  Map<String,Object>? showExtraInfoMap;
  RewardVideoAd._(
      {required String placementId,
      required this.adLoadCallback})
      : super(placementId: placementId);

  static load(
      {required String placementId,
      required AdLoadCallback<RewardVideoAd> adLoadCallback}) {
    RewardVideoAd rewardVideoAd = RewardVideoAd._(
        placementId: placementId, adLoadCallback: adLoadCallback);
    SourceAdType source;
    switch(AdGlobalParamsManager.instance.getAdSource()){

      case AdSource.topOn:
        source = SourceAdType.topOnRewardVideoAd;
        break;
      case AdSource.google:
        source = SourceAdType.adMobRewardVideoAd;
        break;
      default:
        source = SourceAdType.topOnRewardVideoAd;
        break;
    }
    rewardVideoAd.loadConfig(source);
    rewardVideoAd.thirdAdParams = ThirdAdParams(
        appId: AdGlobalParamsManager.instance.getAdAppId(),
        appKey: AdGlobalParamsManager.instance.getAdAppKey(),
        userId: AdGlobalParamsManager.instance.getUseId(),
        channel: AdGlobalParamsManager.instance.getChannel(),
        subChannel: AdGlobalParamsManager.instance.getSubChannel(),
        placementId: placementId,
        thirdAdEventCallback:
            (AdCallbackEvent adCallbackEvent, String message,Object extraInfo) {
          debugPrint("ad callback $adCallbackEvent message:$message");
          switch(adCallbackEvent){
            case AdCallbackEvent.adLoadFail:
              LoadAdError loadAdError = LoadAdError(-1, message);
              rewardVideoAd.adLoadCallback.onAdFailLoad(rewardVideoAd,loadAdError);
              break;
            case AdCallbackEvent.adLoaded:
              if(Platform.isAndroid) {
                final String extra = extraInfo as String;
                final Map<String, Object> map = Map<String, Object>.from(
                    jsonDecode(extra));
                rewardVideoAd.loadExtraInfoMap = map;
              }else if(Platform.isIOS) {
                final Map<String, Object> map = Map<String, Object>.from(
                    extraInfo as Map);
                rewardVideoAd.loadExtraInfoMap = map;
              }
              rewardVideoAd.adLoadCallback.onAdLoaded(rewardVideoAd);
              break;
            case AdCallbackEvent.adOnShow:
              rewardVideoAd.showExtraInfoMap = Map<String,Object>.from(extraInfo as Map);
              rewardVideoAd.rewardVideoContentCallback?.onAdShow(rewardVideoAd);
              break;
            case AdCallbackEvent.adOnDismissed:
              rewardVideoAd.rewardVideoContentCallback?.onAdDismiss(rewardVideoAd);
              break;
            case AdCallbackEvent.adOnClicked:
              rewardVideoAd.rewardVideoContentCallback?.onAdClick(rewardVideoAd);
              break;
            case AdCallbackEvent.adOnReward:
              rewardVideoAd.rewardVideoContentCallback?.onAdReward(rewardVideoAd);
              break;
            case AdCallbackEvent.adShowFail:
              ShowAdError showAdError = ShowAdError(-1, message);
              rewardVideoAd.rewardVideoContentCallback?.onPlayError(rewardVideoAd,showAdError);
              break;
            default:
              LoadAdError loadAdError = LoadAdError(-1, message);
              rewardVideoAd.adLoadCallback.onAdFailLoad(rewardVideoAd,loadAdError);
              break;
          }
        });
    // adThirdInstance.addInstance();
    if (thirdAdInstance.adSourceMap.containsKey(rewardVideoAd.sourceType)) {
      rewardVideoAd.thirdAd = thirdAdInstance
          .adSourceMap[rewardVideoAd.sourceType](rewardVideoAd.thirdAdParams);
      rewardVideoAd.thirdAd.load();
    }
  }
  void play(){
    show();
  }
}
